import isObject from 'lodash/isObject';
import createAPI from 'shared/restClient/CreateAPI';
import toQueryString from 'shared/utils/toQueryString';

const wxRedirect_url = encodeURI(`${location.origin}${location.pathname}?redirect_url=checkPayStatus`);

export const addToCart = createAPI('post', '/jiekec/index.php?route=checkout/cart/add', false);
export const fetchAddresslistApi = createAPI('post', 'jiekec/index.php?route=account/address/addresslist');
export const fetchCategoryList = createAPI('post', 'jiekec/index.php?route=catalog/category/categorylist', false);
export const buyProductApi = createAPI('post', 'jiekec/index.php?route=checkout/buy/product');
export const addWishApi = createAPI('post', 'jiekec/index.php?route=account/wishlist/addwish');
export const delWishApi = createAPI('post', 'jiekec/index.php?route=account/wishlist/delwish');
export const bindCouponApi = createAPI('post', 'jiekec/index.php?route=account/coupon/bindcoupon');
export const fetchProvinceListApi = createAPI('post', 'jiekec/index.php?route=extension/area/provincelist', false);
export const fetchCitylistListApi = createAPI('post', 'jiekec/index.php?route=extension/area/citylist', false);
export const fetchCountyListApi = createAPI('post', 'jiekec/index.php?route=extension/area/countylist', false);
export const fetchcancelOrderApi = createAPI('post', 'jiekec/index.php?route=account/order/cancelorder');
export const checkPaymentStatusApi = createAPI('post', 'jiekec/index.php?route=payment/query/ispaid');
export const expressChargeApi = createAPI('post', 'jiekec/index.php?route=extension/express/charge', true, true);
export const wxpayApi = createAPI('post', `jiekec/index.php?route=payment/wxpay/qrcode&redirect_url=${wxRedirect_url}`);
export const logoutApi = createAPI('post', 'jiekec/index.php?route=account/logout', false);
export const alipayApi = ({orderId, uid, token }) => `${window.url ? window.url : ''}jiekec/index.php?route=payment/alipay/pagepay&order_id=${orderId}&uid=${uid}&token=${token}&alipayh5=1`;
export const appid = 'wx44ae624bf638d218'

let setDoMain;
if (process.env.NODE_ENV === 'development') {
  setDoMain = 'https://ssocanon.st.beijing-dentsu.com.cn'
} else {
  setDoMain = 'https://clubcanon.stage.beijing-dentsu.com.cn'
}

let ssoConfig = {
  doMain: setDoMain,
  appId: 'app_id=EC'
}

if (isObject(window.ssoConfig)) {
  ssoConfig = { ...ssoConfig, ...window.ssoConfig };
}
const { doMain, appId } = ssoConfig;

export const getSsoApi = (sessionid) => `${doMain}/sso/auth/visit?${appId}&sessionid=${sessionid}`;
export const getVerifyTicketApi = ({ sessionid, ticket }) => `${doMain}/sso/auth/verifyTicket?${appId}&ticket=${ticket}&sessionid=${sessionid}`;
export const getEditPhoneApi = (parmas) => `${doMain}/sso/auth/changeMobile?${appId}&${toQueryString(parmas)}`;
export const getAuthSsoApi = ({ sessionid, account }) => `${doMain}/sso/auth?${appId}&account=${account}&sessionid=${sessionid}&need_re_auth=1`;
export const getEditPasswordApi = (parmas) => `${doMain}/sso/auth/modifyPassword?${appId}&${toQueryString(parmas)}`;